/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.click.app.webapp.action;

import com.click.app.DateFormatChanger;
import com.click.app.model.Coin;

import com.click.app.model.Purchase;
import com.click.app.model.PurchasePayment;
import com.click.app.service.GenericManager;
import com.click.app.service.PurchaseManager;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;

/**
 *
 * @author Joel Chuca Condo
 */
public class PurchasePaymentAction extends BaseAction {

    private PurchaseManager purchaseManager;
    private GenericManager<PurchasePayment, Long> purchasePaymentManager;
    private GenericManager<Coin, Long> coinManager;
    private Purchase purchase;
    private List purchases;
    private List purchasePaymetns;
    private PurchasePayment purchasePayment;
    private Long id;
    private Long idPurchase;
    private String paidDate;

    private String amountTooltip;
     private String codeTooltip;
    //DateFormatChanger changer = new DateFormatChanger();

    public void setPurchasePaymentManager(GenericManager<PurchasePayment, Long> purchasePaymentManager) {
        this.purchasePaymentManager = purchasePaymentManager;
    }

    public void setCoinManager(GenericManager<Coin, Long> coinManager) {
        this.coinManager = coinManager;
    }

    public void setPurchaseManager(PurchaseManager purchaseManager) {
        this.purchaseManager = purchaseManager;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long setIdPurchase() {
        return idPurchase;
    }

    public Purchase getPurchase() {
        return purchase;
    }

    public void setPurchase(Purchase purchase) {
        this.purchase = purchase;
    }

    public List getPurchases() {
        return purchases;
    }

    public List getPurchasePaymetns() {
        return purchasePaymetns;
    }

    public void setPurchasePayment(PurchasePayment purchasePayment) {
        this.purchasePayment = purchasePayment;
    }

    public PurchasePayment getPurchasePayment() {
        return purchasePayment;
    }

    public String getPaidDate() {
        paidDate = DateFormatChanger.changeDatetoString(purchasePayment.getDate());
        return paidDate;
    }

    public void setPaidDate(String paidDate) {
        this.paidDate = paidDate;
        purchasePayment.setDate(DateFormatChanger.changeStringtoDate(paidDate));
    }

    public String purchaseList() {
        purchases = purchaseManager.findPurchasesUnPaid();
        return SUCCESS;
    }

    public String getAmountTooltip() {
        return amountTooltip;
    }

    public String getCodeTooltip() {
        return codeTooltip;
    }
    public Double getRp(){
        return ((Purchase)getSession().getAttribute("purchase")).getRestPrice();
    }

    public String list() {
        if(id != null){
            purchase = purchaseManager.get(id);
            purchase.getPurchaseDetails();
            getSession().setAttribute("purchase", purchase);

        }else{
            purchase = (Purchase)getSession().getAttribute("purchase");
            purchase = (Purchase)purchaseManager.get(purchase.getId());
            purchase.getPurchaseDetails();
            getSession().setAttribute("purchase", purchase);
        }
        purchasePaymetns = purchase.getPurchasePayments();        
        return SUCCESS;
    }

    public String edit() {
        
        if (id != null) {
            purchasePayment = purchasePaymentManager.get(id);
        } else {
            purchasePayment = new PurchasePayment();
            purchasePayment.setDate(new Date());            
        }
        
        codeTooltip = getText("purchasePayment.code.tooltip");
        amountTooltip = getText("purchasePayment.amount.tooltip");
        
        return SUCCESS;
    }

    public String save() throws Exception {
        
        if (cancel != null) {
            return CANCEL;
        }
        if (delete != null) {
            //return delete();
        }        

        boolean isNew = false;
        isNew = (purchasePayment.getId() == null);

        purchase = (Purchase)getSession().getAttribute("purchase");
        purchase = purchaseManager.get(purchase.getId());

        Double mount = Double.parseDouble(getRequest().getParameter("purchasePayment.amount"));

        // Verify that the new mount not exceed the rest price
        if(! isNew ){
            PurchasePayment purchasePaymentAux = purchasePaymentManager.get(purchasePayment.getId());
            Double modifyMount = purchase.getRestPrice() + purchasePaymentAux.getAmount();
            if(mount > modifyMount){
                errorMessage("The mount: "+mount+" exceed the rest price");
                return "reload";
            }else {
                purchase.setRestPrice(modifyMount);
                purchase = purchaseManager.save(purchase);           
            }
        }else{
            if(mount > purchase.getRestPrice()){
                errorMessage("The mount: "+mount+" exceed the rest price");
                return "reload";
            }
        }
        
        purchasePayment.setDate(DateFormatChanger.changeStringtoDate(paidDate));
        purchasePayment.setAmount(Double.parseDouble(getRequest().getParameter("purchasePayment.amount")));
        purchasePayment.setPurchase(purchase);
        purchasePayment.setTypePayment(0);
        purchasePayment.setAmountLeft(purchase.getRestPrice()-purchasePayment.getAmount());
        purchasePayment=purchasePaymentManager.save(purchasePayment);
        
        purchase.setRestPrice(purchase.getRestPrice()-purchasePayment.getAmount());
        if(purchase.getDeleted()){
            purchase.setDeleted(false);
            purchase.updateDeleted(false);
        }
        purchase = purchaseManager.save(purchase);
        
        if (!isNew) {
            saveMessage(getText("purchasePayment.updated"));
            return INPUT;
        } else {
            saveMessage(getText("purchasePayment.added"));
            return SUCCESS;
        }
    }

    public String delete(){
        purchasePayment = purchasePaymentManager.get(purchasePayment.getId());
        purchasePaymentManager.remove(purchasePayment.getId());

        purchase = (Purchase)getSession().getAttribute("purchase");
        purchase = (Purchase)purchaseManager.get(purchase.getId());
        purchase.setRestPrice(purchase.getRestPrice()+purchasePayment.getAmount());        
        purchase = purchaseManager.save(purchase);
        return "delete";
    }

    public String finalizePurchasePayment(){
        getSession().setAttribute("purchase", null);
        return SUCCESS;
    }
}

